package cn.edu.buu.news.controller;

import cn.edu.buu.news.entity.Tool;
import cn.edu.buu.news.service.ToolService;
import cn.edu.buu.news.vo.PageQuery;
import cn.edu.buu.news.vo.PageResult;
import cn.edu.buu.news.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/tool")
public class ToolController {

    @Autowired
    private ToolService toolService;

    // 新增工具
    @PostMapping
    public Result add(@RequestBody Tool tool) {
        toolService.add(tool);
        return Result.success();
    }

    // 删除工具
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        toolService.deleteById(id);
        return Result.success();
    }

    // 更新工具
    @PutMapping
    public Result update(@RequestBody Tool tool) {
        toolService.update(tool);
        return Result.success();
    }

    // 根据ID查询工具
    @GetMapping("/{id}")
    public Result<Tool> getById(@PathVariable Long id) {
        return Result.success(toolService.findById(id));
    }

    // 查询所有工具
    @GetMapping
    public Result<List<Tool>> getAll() {
        return Result.success(toolService.findAll());
    }

    // 分页查询工具
    @PostMapping("/list")
    public Result<PageResult<Tool>> getByPage(@RequestBody PageQuery query) {
        return Result.success(toolService.findByPage(query));
    }
}